package com.dingding.service.user.core.biz.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.dingding.common.core.beans.UserAttached;
import com.dingding.facade.user.dto.GetUserAttachedDTO;
import com.dingding.service.user.core.biz.IUserAttachedService;
import com.dingding.service.user.core.dao.IUserAttachedDao;
import com.dingding.service.user.core.enums.UserAttachedKeyEnum;

/**
 * 
 * @ClassName: IUserAttachedService 
 * @Description:用户属性附属表操作Service
 * @author CHEHAUNBO 
 * @date 2016年4月1日 下午3:51:16
 * 
 */
@Component("userAttachedService")
public class UserAttachedServiceImpl implements IUserAttachedService {

	@Resource(name = "userAttachedDao")
	private IUserAttachedDao userAttachedDao;

	/**
	 * 
	 * @Title: getUserAttached 
	 * @Description: 获取用户附属属性值
	 * @param  getUserAttachedDTO 参数传递DTO
	 * @return UserAttached  用户附属属性对象
	 * @author CHEHAUNBO
	 * 
	 */
	public int getUserAttached(GetUserAttachedDTO getUserAttachedDTO) {

		// 用户ID
		String userId = getUserAttachedDTO.getUserId();

		UserAttached userAttached = userAttachedDao.getUserAttached(userId, UserAttachedKeyEnum.ISATTENDACTIVITY);

		if (userAttached == null) { // 不存在要查询的用户属性
			// 如果不存在，先添加用户附属属性
			boolean isAddSuccess = userAttachedDao.addUserAttached(userId, UserAttachedKeyEnum.ISATTENDACTIVITY);
			if (isAddSuccess) {
				return 1;
			} else {
				return 2;
			}
		} else {
			return userAttached.getAttachedkeyValue();
		}

	}

	/**
	 * 
	 * @Title: attendActivity 
	 * @Description: 签到
	 * @param getUserAttachedDTO 参数传递DTO
	 * @return boolean true 成功 false 失败
	 * @author CHEHAUNBO
	 * 
	 */
	@Override
	public boolean attendActivity(GetUserAttachedDTO getUserAttachedDTO) {

		// 用户ID
		String userId = getUserAttachedDTO.getUserId();

		int status = this.getUserAttached(getUserAttachedDTO);

		boolean isSuccess = false;

		if (status == 1) { // 未签到
			isSuccess = userAttachedDao.updateUserAttached(userId, UserAttachedKeyEnum.ISATTENDACTIVITY, 2);
		} else { // 已签到，直接返回无需再次签到
			return true;
		}

		return isSuccess;
	}

	/**
	 * @Title: resetAttendActivity
	 * @Description: 重置签到
	 * @author yuHaiQing
	 * @return boolean    true 重置签到成功 false 重置签到失败
	 */
	@Override
	public boolean resetAttendActivity() {

		return userAttachedDao.resetUserAttached();
	}

}
